package com.tedu.demo1;

import java.sql.*;
import java.util.Date;

/**
 * Author：毕龙
 * Date: 2024/6/14 9:24
 */
public class TestJdbc3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        User user = new User(4, "赵六'", "123121", new Date());

        //1.获取驱动Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Chongqing";
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url,username,password );

        //sql语句
        //sql语句
        String sql = "insert into user(id, username, password, birthday) values (?,?,?,?)";
        //3.获取Statement语句
        PreparedStatement pstm = connection.prepareStatement(sql);

        //将utilDate改编为sqlDate
        Date birthday = user.getBirthday();
        long time = birthday.getTime();
        java.sql.Date date = new java.sql.Date(time);


        //给？设置值
        pstm.setInt(1,user.getId());
        pstm.setString(2,user.getUsername());
        pstm.setString(3,user.getPassword());
        pstm.setDate(4,date);


        //4.执行sql语句
        int i = pstm.executeUpdate();
        //如果为false表示执行的是增删改
        System.out.println(i);
        //5.资源释放
        pstm.close();
        connection.close();

    }
}
